/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.constant.KwConstant;
import me.zhengjie.domain.KwExam;
import me.zhengjie.exception.ParameterException;
import me.zhengjie.pojo.entity.Exam;
import me.zhengjie.service.KwExamRoomService;
import me.zhengjie.service.KwExamService;
import me.zhengjie.service.KwExamStudentService;
import me.zhengjie.service.KwExamTeacherService;
import me.zhengjie.service.dto.KwExamQueryCriteria;
import me.zhengjie.service.dto.KwExamRoomQueryCriteria;
import me.zhengjie.service.dto.KwExamStudentQueryCriteria;
import me.zhengjie.service.dto.KwExamTeacherQueryCriteria;
import me.zhengjie.util.BaseResponse;
import me.zhengjie.util.KwUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author LongYang
 * @website https://el-admin.vip
 * @date 2021-06-28
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "考试信息前端控制器管理")
@RequestMapping("/api/kwExam")
@Slf4j
public class KwExamController {

    private final KwExamService kwExamService;
    private final KwExamRoomService kwExamRoomService;
    private final KwExamStudentService kwExamStudentService;
    private final KwExamTeacherService kwExamTeacherService;
    private final KwUtils kwUtils;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('kwExam:list')")
    public void download(HttpServletResponse response, KwExamQueryCriteria criteria) throws IOException {
        String identity = kwUtils.identity();
        if (Objects.equals(identity, KwConstant.DEPT)) return;
        kwExamService.download(kwExamService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询考试信息前端控制器")
    @ApiOperation("查询考试信息前端控制器")
    @PreAuthorize("@el.check('kwExam:list')")
    public ResponseEntity<Object> query(KwExamQueryCriteria criteria, Pageable pageable) {
        kwUtils.hasOtherPermissionGeneralId(criteria.getGeneralId());
        return new ResponseEntity<>(kwExamService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增考试信息前端控制器")
    @ApiOperation("新增考试信息前端控制器")
    @PreAuthorize("@el.check('kwExam:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody KwExam resources) {

        Integer id = resources.getGeneralId();
        kwUtils.hasOtherPermissionGeneralId(id);
        return new ResponseEntity<>(kwExamService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改考试信息前端控制器")
    @ApiOperation("修改考试信息前端控制器")
    @PreAuthorize("@el.check('kwExam:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody KwExam resources) {
        Integer id = resources.getId();
        boolean permission = kwUtils.hasOtherPermission(Arrays.asList(id));
        if (!permission) throw new ParameterException("无权操作");
        kwExamService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除考试信息前端控制器")
    @ApiOperation("删除考试信息前端控制器")
    @PreAuthorize("@el.check('kwExam:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        kwExamService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /*-------------------------------------------------------------------------------------------*/
    @ApiOperation("分页查询考试的考场信息")
    @GetMapping("/find-All-ExamRoom")
    public ResponseEntity findAllExamRoom(KwExamRoomQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(kwExamRoomService.queryAll(criteria, pageable), HttpStatus.OK);

    }

    @ApiOperation("分页查询所有考场考生信息")
    @GetMapping("/find-All-ExamStudent")
    @ResponseStatus
    public ResponseEntity findAllExamStudent(KwExamStudentQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(kwExamStudentService.queryAll(criteria, pageable), HttpStatus.OK);

    }

    @ApiOperation("分页查询所有考场监考老师信息")
    @GetMapping("/find-All-ExamTeacher")
    public ResponseEntity findAllExamTeacher(KwExamTeacherQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(kwExamTeacherService.queryAll(criteria, pageable), HttpStatus.OK);
    }


    @ApiOperation("考试信息导出")
    @PostMapping("/ruoyiExport")
    @PreAuthorize("@el.check('kwExam:export')")
    public void export(HttpServletResponse response, @RequestBody List<Exam> exams) throws Exception {
        String identity = kwUtils.identity();
        if (Objects.equals(identity, KwConstant.DEPT)) throw new ParameterException("无权操作");
        try {
            kwExamService.export(response, exams);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("考试信息导出接口：" + e.getMessage());
        }
    }

    @ApiOperation("查询总考试下的的所有考试不分页")
    @GetMapping("/queryAll")
    @ApiImplicitParam(name = "generalId", value = "总考试id", required = true)
    public BaseResponse findAll(@RequestParam("generalId") Long generalId) {
        return BaseResponse.ok(kwExamService.findALl(generalId));
    }


}